package com.ytg.apt;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.ytg.apt.databinding.ActivityMainBinding;
import com.ytg.myanno.BindView;
import com.ytg.myanno.OnClick;
import com.ytg.myapi.BindWay;

import org.json.JSONObject;

import java.lang.reflect.Method;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.btn1)
    public Button mBtn;
    @BindView(R.id.btn2)
    public Button mBtn2;
    @BindView(R.id.btn3)
    public Button mBtn3;
    @BindView(value = R.id.metlis, key = "dataet")
    EditText metlis;
    @BindView(value = R.id.mtvlis, key = "dataTv")
    TextView mtvlis;
    @BindView(value = R.id.mtvlis2, key = "dataTv2")
    TextView mtvlis2;
    @BindView(value = R.id.mview, key = "dataView")
    MView mview;
    @BindView(value = R.id.mrg, key = "rbValue")//
    RadioGroup mrg;
//    @BindView(value = R.id.mRb1)
//    RadioButton mRb1;
//    @BindView(value = R.id.mRb2)
//    RadioButton mRb2;

    @BindView(value = R.id.mchbox, key = "box")
    CheckBox box;
    @BindView(value = R.id.mboxs, key = "boxm")
    CustomCheckBox mboxs;
    //    LinkedHashMap<Integer, String> mapViewFeild = new LinkedHashMap<>();
//    LinkedHashMap<String, View> mapFeildView = new LinkedHashMap<>();
//    ObservableArrayMap<String, String> mapViewData = new ObservableArrayMap<>();
    ActivityMainBinding vdb;
    MainActivity$$Injector injector;
//    ObservableField<String> field = new ObservableField<>();
//    @Override
//    public androidx.appcompat.app.AppCompatDelegate getDelegate() {
//        return SkinAppCompatDelegateImpl.get(this, this);
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//        LayoutInflaterCompat.setFactory2(LayoutInflater.from(this),new LayoutInflater.Factory2(){
//            @Override
//            public View onCreateView(String name, Context context, AttributeSet attrs) {
//                return onCreateView(null,"",context,attrs);
//            }
//            @Override
//            public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
//                Log.e("替换","----"+name);
//                switch(name){
//                    case "TextView":
//                        return  new com.ytg.myapi.view.SkinCompatTextView(context, attrs);
//                    case "EditText":
//                        return  new com.ytg.myapi.view.SkinCompatEditText(context, attrs);
//                    case "RadioGroup":
//                        return  new com.ytg.myapi.view.SkinCompatRadioButton(context, attrs);
//                }
//
//                return getDelegate().createView(parent,name,context,attrs);
//            }
//        });
        ((MainActivity$$Injector)BindWay.getInstance().getInjector(this)).init(this);
        super.onCreate(savedInstanceState);
        Method inflate = null;
        try {
            inflate = ActivityMainBinding.class.getDeclaredMethod("inflate", LayoutInflater.class);
            vdb = (ActivityMainBinding) inflate.invoke(null, getLayoutInflater());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setContentView(vdb.getRoot());

//        setContentView(R.layout.activity_main);
        injector = ((MainActivity$$Injector) BindWay.getInstance().bind(this));
//        injector.mapViewData.addOnMapChangedCallback(new androidx.databinding.ObservableMap.OnMapChangedCallback<androidx.databinding.ObservableMap<String, String>, String, String>() {
//            @Override
//            public void onMapChanged(ObservableMap<String, String> sender, String key) {
//                if (injector.mapFeildView.get(key) instanceof EditText) {
//                    if (!(((EditText) injector.mapFeildView.get(key))).getText().toString().equals(sender.get(key))) {
//                        (((EditText) injector.mapFeildView.get(key))).setText(sender.get(key));
//                        (((EditText) injector.mapFeildView.get(key))).setSelection(sender.get(key).length());
//                    }
//                } else if (injector.mapFeildView.get(key) instanceof TextView) {
//                    if (!(((TextView) injector.mapFeildView.get(key))).getText().toString().equals(sender.get(key))) {
//                        (((TextView) injector.mapFeildView.get(key))).setText(sender.get(key));
//                    }
//                }
//                else if (injector.mapFeildView.get(key) instanceof RadioGroup) {
//                    RadioGroup group = (RadioGroup) injector.mapFeildView.get(key);
//                    for (int i = 0; i < group.getChildCount(); i++) {
//                        if(((RadioButton)group.getChildAt(i)).getText().toString().equals(sender.get(key))){
//                            if(group.getCheckedRadioButtonId()!=group.getChildAt(i).getId()){
//                                group.check(group.getChildAt(i).getId());
//                            }
//                        }
//                    }
//                } else {
//                    try {
//                       getChildViewTag(injector.mapFeildView.get(key));
//                       if(null!=sonview){
//                           String con =sonview.getText().toString();
//                           if (!(null != con && con.equals(sender.get(key)))) {
//                               sonview.setText(sender.get(key));
//                           }
//                       }
//
////                        Class<?> mClass = Class.forName(injector.mapFeildVType.get(key));
////                        if (checkMethod(mClass, injector.mapMethodGet.get(key)) && checkMethod(mClass, injector.mapMethodSet.get(key))) {
////                            Method getText = mClass.getMethod(injector.mapMethodGet.get(key));
////                            String content = (String) getText.invoke(injector.mapFeildView.get(key));
////                            if (!(null != content && content.equals(sender.get(key)))) {
////                                Method setText = mClass.getMethod(injector.mapMethodSet.get(key), String.class);
////                                setText.invoke(injector.mapFeildView.get(key), sender.get(key));
////                            }
////                        } else {
////                            Log.e("bindview", injector.mapMethodGet.get(key) + "或" + injector.mapMethodSet.get(key) + "方法不存在");
////                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
        injector.setData("dataet", "于");
        injector.setData("dataTv", "于2");
        injector.setData("dataTv2", "1");
        injector.setData("dataView", "123");
        injector.setData("rbValue", "是");
//        ArrayList<TextView> sonview;
//        for (int i = 0; i <sonview.size() ; i++) {
//            String con =sonview.get(i).getText().toString();
//            if(sonview.get(i) instanceof RadioButton){
//                if (!(null != con && con.equals(sender.get(key)))) {
//                    ((RadioButton)sonview.get(i)).setChecked(true);
//                }else{
//                    ((RadioButton)sonview.get(i)).setChecked(false);
//                }
//            }else{
//
//                           if (!(null != con && con.equals(sender.get(key)))) {
//                               sonview.get(i).setText(sender.get(key));
//                           }
//            }
//
//        }
        box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                injector.setData(buttonView.getTag().toString().replace("BTag:", ""), isChecked+"");
            }
        });
        mboxs.addBox("1","选项1",false).addBox("2","选项2",true).addBox("3","选项3",true).addBox("4","选项4",false).build();
    }
//    TextView sonview = null;
//    void getChildViewTag(View view) {
//
//        if (view instanceof ViewGroup) {
//            ViewGroup group = (ViewGroup) view;
//            for (int i = 0, count = group.getChildCount(); i < count; i++) {
//                 if (group.getChildAt(i) instanceof TextView ) {
//                    if (null != group.getChildAt(i).getTag() && group.getChildAt(i).getTag().toString().startsWith("BTag:")) {
//                        sonview = (TextView) group.getChildAt(i);
//
//                        return;
//                    }
//                }else{
//                    getChildViewTag(group.getChildAt(i));
//                }
//            }
//        }
////        return sonview;
//    }

    boolean checkMethod(Class<?> mClass, String mName) {
        Method[] method = mClass.getDeclaredMethods();
        for (int i = 0, count = method.length; i < count; i++) {
            if (method[i].getName().equals(mName)) {
                return true;
            }
        }
        return false;
    }

    @OnClick({R.id.btn1})
    public void click() {
        vdb.mtvlis.setText("123");
//        Toast.makeText(this, injector.mapViewData.toString(), Toast.LENGTH_SHORT).show();
//        mapViewData.put("dataet", "123");
//        mBtn.setText("123");

    }

    @OnClick({R.id.btn2})
    public void click2() {
        injector.setData("dataView", "456");
        injector.setData("box", "勾选");
//        vdb.mview.setText("123");
//        vdb.mtvlis2.setText("456");
//        Toast.makeText(this, injector.mapViewData.toString(), Toast.LENGTH_SHORT).show();
//        injector.mapViewData.put("dataTv", "456");
    }

    @OnClick({R.id.btn3})
    public void click3() {
        try {
            Toast.makeText(this, new JSONObject(injector.getData().toString()).toString(), Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}